/**
 * 11选5的规则(基类)
 */
var eleven5Rule = function() {
    /* 投注名称与服务器名称的对应关系
     "总和大" ：{
        display: "总和 - 大",
     }
    */
    this.map = {

    };
    var arr = ['大', '小', '单', '双'];
    var arr2 = [1.99, 1.99, 1.97, 1.97];
    for (var i in arr) {
        this.map['和' + arr[i]] = {
            display: '总和 - ' + arr[i],
            rate: arr2[i],
        }
    }
    var arr = ['龙', '虎'];
    for (var i in arr) {
        this.map[arr[i]] = {
            display: arr[i],
            rate: 1.99,
        }
    }
    this.map['尾大'] = {
        display: '总和尾 - 大',
        rate: 1.938,
    }
    this.map['尾小'] = {
        display: '总和尾 - 小',
        rate: 1.938,
    }

    var arr = ['大', '小', '单', '双'];
    var arr2 = ['第一球', '第二球', '第三球', '第四球', '第五球'];
    var arr3 = ['一', '二', '三', '四', '五'];
    for (var i in arr2) {
        for (var j in arr) {
            this.map[arr3[i] + arr[j]] = {
                display: '第' + arr3[i] + "球 - " + arr[j],
                rate: 1.99,
            }
        }
        for (var j = 1; j <= 11; j++) {
            var k = j < 10 ? ("0" + j) : j;
            this.map[arr3[i] + k] = {
                display: '第' + arr3[i] + "球 - " + j,
                rate: 9.9,
            }
        }
    }

    this.map['任1'] = {
        display: "一中一",
        rate: 1.92,
    }
    this.map['任2'] = {
        display: "二中二",
        rate: 5.1,
    }
    this.map['任3'] = {
        display: "三中三",
        rate: 14,
    }
    this.map['任4'] = {
        display: "四中四",
        rate: 50,
    }
    this.map['任5'] = {
        display: "五中五",
        rate: 410,
    }
    this.map['任6'] = {
        display: "六中五",
        rate: 60,
    }
    this.map['任7'] = {
        display: "七中五",
        rate: 15,
    }
    this.map['任8'] = {
        display: "八中五",
        rate: 6.5,
    }

    this.map['组2'] = {
        display: "前二组选",
        rate: 40,
    }
    this.map['组3'] = {
        display: "前三组选",
        rate: 140,
    }
    this.map['直2'] = {
        display: "前二直选",
        rate: 90,
    }
    this.map['直3'] = {
        display: "前三直选",
        rate: 890
    }
};
eleven5Rule.prototype = {};

eleven5Rule.prototype.isOdd = function(v) {
    return v % 2 == 1 && v != 11;
};
eleven5Rule.prototype.isEven = function(v) {
    return v % 2 == 0 && v != 11;
}
eleven5Rule.prototype.isBig = function(v) {
    return v >= 6 && v != 11;
}
eleven5Rule.prototype.isSmall = function(v) {
    return v < 6;
}
eleven5Rule.prototype.isLong = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 > a2;
}
eleven5Rule.prototype.isHu = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 < a2;
}
eleven5Rule.prototype.isHe = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 == a2;
}
eleven5Rule.prototype.isHeOdd = function(list) {
    var total = this.he(list);
    return total % 2 == 1;
}
eleven5Rule.prototype.isHeBig = function(list) {
    var total = this.he(list);
    return total > 30;
}
eleven5Rule.prototype.isHeSmall = function(list) {
    var total = this.he(list);
    return total < 30;
}
eleven5Rule.prototype.isHeWeiBig = function(list) {
    var total = this.he(list);
    total = total % 10;
    return total >= 5;
}

// 计算总和
eleven5Rule.prototype.he = function(list) {
    var total = 0;
    for (var i in list) {
        total += parseInt(list[i]);
    }
    return total;
}

/**
 * 计算露珠
 */
eleven5Rule.prototype.luzhu = function(list, qiu, key) {
    // 将不是今天的开奖过滤掉
    var list2 = [];
    var today = new Date().Format("yyyy-MM-dd");
    for (var i in list) {
        if (list[i].time.startsWith(today)) list2.push(list[i]);
    }

    var self = this;
    if (key == 'count') {
        // 各数字的开奖次数
        var m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            m[n - 1]++;
        }
        return m;
    }
    if (key == 'number') {
        // 号码排盘(最多25列)
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "daxiao") {
        // 大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = self.isBig(n) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuang") {
        // 单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = n % 2 == 0 ? "双" : "单";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedaxiao") {
        var m = [];
        for (var i in list2) {
            var n = self.isHeBig(list2[i].red) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedanshuang") {
        var m = [];
        for (var i in list2) {
            var n = self.isHeOdd(list2[i].red) ? "单" : "双";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "heweishudaxiao") {
        // 总和尾数大小
        var m = [];
        for (var i in list2) {
            var n = self.isHeWeiBig(list2[i].red) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "longhu") {
        // 龙虎
        var m = [];
        for (var i in list2) {
            var n = '和';
            if (self.isLong(list2[i].red)) n = '龙';
            else if (self.isHu(list2[i].red)) n = '虎';
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
}

/**
 * 计算长龙信息
 */
eleven5Rule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 总和: 大、小、单、双，龙 虎
    var l0 = list[0].red;

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i].red;
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    if (self.isHeBig(l0)) {
        f("总和 - 大", function(l) {
            return self.isHeBig(l);
        });
    }
    else if (self.isHeSmall(l0)) {
        f("总和 - 小", function(l) {
            return self.isHeSmall(l);
        });
    }
    if (self.isHeOdd(l0)) {
        f("总和 - 单", function(l) {
            return self.isHeOdd(l);
        });
    }
    else {
        f("总和 - 双", function(l) {
            return !self.isHeOdd(l);
        });
    }
    if (self.isLong(l0)) {
        f("龙", function(l) {
            return self.isLong(l);
        });
    }
    else if (self.isHu(l0)) {
        f("虎", function(l) {
            return self.isHu(l);
        });
    }
    if (self.isHeWeiBig(l0)) {
        f("总和尾大", function(l) {
            return self.isHeWeiBig(l);
        });
    }
    else {
        f("总和尾小", function(l) {
            return !self.isHeWeiBig(l);
        });
    }

    // 1-5球大小、单双
    var arr = ["第一球", "第二球", "第三球", "第四球", "第五球"];
    for (var index = 0; index < 5; index++) {
        var n = parseInt(l0[index]);
        if (self.isBig(n)) {
            f(arr[index] + " - 大", function(l) {
                return self.isBig(parseInt(l[index]));
            });
        }
        else if (self.isSmall(n)) {
            f(arr[index] + " - 小", function(l) {
                return self.isSmall(parseInt(l[index]));
            });
        }
        if (self.isOdd(n)) {
            f(arr[index] + " - 单", function(l) {
                return self.isOdd(parseInt(l[index]));
            });
        }
        else if (self.isEven(n)) {
            f(arr[index] + " - 双", function(l) {
                return self.isEven(parseInt(l[index]));
            });
        }
    }

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称
 */
eleven5Rule.prototype.getTargetName = function(name) {
    var arr = name.split(":");
    if (arr.length == 1) {
        if (!this.map[name]) {
            console.warn("不存在的项目", name);
            return name;
        }
        return this.map[name].display;
    }
        
    return this.map[arr[0]].display + " &nbsp;<span class='blue' style='font-size:12px;'>" + arr[1] + "</span>";
};
